package app.controllers.customer;

import app.dtos.GameWinGift;
import app.models.game.GameJoinrecord;
import com.github.sog.controller.AjaxMessage;
import com.github.sog.controller.BasicController;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * .
 * </p>
 *
 * @author Jerry Ou
 * @version 1.0 2014-04-30 09:04
 * @since JDK 1.6
 */
public class GiftController extends BasicController {

    /**
     * The index route.
     */
    public void index() {
        render("gift.ftl");
    }

    public void searchGift()
    {
        String query = getPara("query");
        String sn = getPara("sn");

        List<GameWinGift> gameWinGifts = GameJoinrecord.dao.gameWinGift(query, sn);
        if (gameWinGifts != null && !gameWinGifts.isEmpty())
        {
            renderJson(AjaxMessage.ok(gameWinGifts.get(0)));
        }
        else
        {
            renderJson(AjaxMessage.failure("没有查询到中奖信息"));
        }
    }

    public void receive()
    {
        try
        {
            long recordId = getParaToLong("recordId", 0l);
            GameJoinrecord joinrecord = GameJoinrecord.dao.findById(recordId);
            joinrecord.set("receive", true);
            joinrecord.set("receive_time", new Date());
            joinrecord.update();
            renderJson(AjaxMessage.ok("领取成功", joinrecord));
        }
        catch (Exception e)
        {
            renderJson(AjaxMessage.error("领取失败"));
        }
    }

}
